package view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import utils.CommandGenerator;
import control.PaymentMethodManager;
import control.Shell;
import control.ValidateInput;
import control.ViewPanelManager;
import domain.PaymentMethod;
import javax.swing.SwingConstants;
import javax.swing.ListSelectionModel;

//@author A0081236L
public class PaymentTypeManagerPanel extends BackgroundPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public 	DefaultComboBoxModel<PaymentMethod> cbm_payType = new DefaultComboBoxModel<PaymentMethod>();
	public 	JList<PaymentMethod> list_payment = new JList<PaymentMethod>();
	public 	JButton btnAdd = new JButton("Add");
	public 	JButton btnEdit = new JButton("Edit");
	public 	JButton btnDelete = new JButton("Delete");
	private JTextField txt_name;
	private PaymentMethodManager _payMan;
	private Shell _shell;
	private ViewPanelManager _tabPanelMgr;
	
	/**
	 * @param payManager
	 * @param importShell
	 * @param panel_xpnManager
	 * @param panel_viewReport
	 */
	public PaymentTypeManagerPanel(PaymentMethodManager payManager, Shell importShell, ViewPanelManager tabPanelMgr)
	{	
	    _payMan = payManager;
		_shell = importShell;
		_tabPanelMgr = tabPanelMgr;
	    
		this.setLayout(null);		
		this.setBounds(0, 0, 719, 424);
		
		
		txt_name = new JTextField();
		txt_name.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
		        	if(e.getKeyCode()==KeyEvent.VK_ENTER)
		        	{
			        	if(txt_name.getText().length()==0)
			        	{
			        		JOptionPane.showMessageDialog(null, "Please enter a category into the text field.");
			        	}	
			        	else
			        	{
			        		if(!ValidateInput.validName(txt_name.getText()))
			        			return;
			        		String cmd = CommandGenerator.getAddPaymentMethodCmd(txt_name.getText());
			        		int res = _shell.readCmd(cmd);
			        		if(res==-1)
			        		{
				        		JOptionPane.showMessageDialog(null, "\""+ txt_name.getText().toUpperCase() + "\"" + " cannot be added.(Existed Already?)");
				        		return;
			        		}
			        		txt_name.setText("");
			        		update();
			        	}
		        	}
				
			}
		});
		txt_name.setBounds(55, 78, 163, 23);
		this.add(txt_name);
		txt_name.setColumns(10);
		list_payment.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		list_payment.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				
			}
		});
		JScrollPane scroll_listpPay = new JScrollPane ( list_payment );
		scroll_listpPay.setAutoscrolls(true);
		scroll_listpPay.setBounds(240, 30, 200, 200);
		this.add(scroll_listpPay);
		
		btnAdd.setBounds(97, 132, 89, 23);
		this.add(btnAdd);
		
		btnAdd.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
	        	if(txt_name.getText().length()==0)
	        	{
	        		JOptionPane.showMessageDialog(null, "Please enter a category into the text field.");
	        	}	
	        	else
	        	{
	        		if(!ValidateInput.validName(txt_name.getText()))
	        			return;
	        		String cmd = CommandGenerator.getAddPaymentMethodCmd(txt_name.getText());
	        		int res = _shell.readCmd(cmd);
	        		if(res==-1)
	        		{
		        		JOptionPane.showMessageDialog(null, "\""+ txt_name.getText().toUpperCase() + "\"" + " cannot be added.(Existed Already?)");
		        		return;
	        		}
	        		txt_name.setText("");
	        		update();
	        	}
			}
			
		});
		
		btnDelete.setBounds(97, 193, 89, 23);
		this.add(btnDelete);
		
		btnDelete.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
	        	if (list_payment.getSelectedIndex()==-1)
	        	{
	        		JOptionPane.showMessageDialog(null, "Please select a payment method to delete.");        			        		
	        	}
	        	else
	        	{
		        	int id  = list_payment.getSelectedValue().get_id();
		        	if(id==0)
		        	{
		        		JOptionPane.showMessageDialog(null, _payMan.get(id).get_name() +" cannot be deleted.");  
		        		return;
		        	}
	        		String cmd = CommandGenerator.getDeletePaymentMethodCmd(id);
	        		_shell.readCmd(cmd);
	        		txt_name.setText("");
	        		update();
	        	}
				
			}
		});
		
		/*btnBack.setBounds(153, 162, 89, 23);
		this.add(btnBack);
		btnBack.addActionListener(e);*/

		btnEdit.setBounds(97, 162, 89, 23);
		this.add(btnEdit);
		
		JLabel lblPaymentMethod = new JLabel("Payment Methods");
		lblPaymentMethod.setHorizontalAlignment(SwingConstants.CENTER);
		lblPaymentMethod.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		lblPaymentMethod.setBounds(43, 31, 187, 23);
		this.add(lblPaymentMethod);
		btnEdit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
	        	if(txt_name.getText().length()==0)
	        	{
	        		JOptionPane.showMessageDialog(null, "Please enter a payment method into the text field.");
	        	}	
	        	else if (list_payment.getSelectedIndex()==-1)
	        	{
	        		JOptionPane.showMessageDialog(null, "Please select a payment method to change.");        			        		
	        	}
	        	else
	        	{
	        		if(!ValidateInput.validName(txt_name.getText()))
	        			return;
		        	int id  = list_payment.getSelectedValue().get_id();
	        		String cmd = CommandGenerator.getEditPaymentMethodCmd(id,txt_name.getText());
	        		int res = _shell.readCmd(cmd);
	        		if(res==-1)
	        		{
		        		JOptionPane.showMessageDialog(null, "\""+ txt_name.getText().toUpperCase() + "\"" + " cannot be added.(Existed Already?)");
		        		return;
	        		}
	        		txt_name.setText("");
	        		update();
	        	}
				
			}
		});
		update();
	}

	//@author A0098891H
	public void update()
	{
		updateList();
		_tabPanelMgr.updatePayMethod();
	}
	
	public void updateList()
	{
		cbm_payType.removeAllElements();
		for(PaymentMethod pay: _payMan.getList())
		{
			cbm_payType.addElement(pay);
		}		
		list_payment.setModel(cbm_payType);		
	}
}
